/**
 * messageStat
 * 
 * @author ZhangDi
 * @version 1.0.0
 */
 
var fn_count=0;
var rateDataTables;
$(function(){
	var date = [];
	var g_cpuData = [];
	var totalPoints = 250;
	
	
	//学员科目统计柱状图
    $("#classTime").empty(); 
    $("#classTime2").empty();
	$.post("/mcode/system/class/queryClassTime",function(data){
		if(data != null && typeof(data) != "undefined"){
			var str = "";
			for(var i = 0; i < data.length; i++){
				if(data[i]!=null)
				str = str + "<option value='"+ data[i].years +"'>"+data[i].years+"</option>"
			}
			$("#classTime").append(str);
			$("#classTime2").append(str);
		};
	});
	classGraph();
	$("#classTime").on('change',function(){
		var years = $("#classTime").val();
		classGraph(years);
	});
	numGraph();
	$("#classTime2").on('change',function(){
		var years = $("#classTime2").val();
		numGraph(years);
	});
	

	
	
	$("#passId").empty(); 
	$.post("/mcode/system/appraisal/queryPassingTime",function(data){
		console.log(data);
		console.log(data[0].years);
		console.log(data.length);
		if(data != null && typeof(data) != "undefined"){
			var str = "";
			for(var i = 0; i < data.length; i++){
				if(data[i]!=null)
				str = str + "<option value='"+ data[i].years +"'>"+data[i].years+"</option>"
			}
			$("#passId").append(str);
		};
	});
	
//	 $.ajax({
//			url:"/mcode/system/appraisal/queryPassingTime",
//			dataType:"json",
//			type:"POST",
//			async:false,
//			success:function(data){
//				if(data != null && typeof(data) != "undefined"){
//					var str = "";
//					for(var i = 0; i < data.length; i++){
//						str = str + "<option value='"+ data[i].years +"'>"+data[i].years+"</option>"
//					}
//					$("#passId").append(str);
//				};
//			}
//	 });
	
	
	   	
	rateDataTables = $('#rateDataTable').DataTable({
		"bProcessing": false,
		"bLengthChange":false,
		"bServerSide": true,
		"bFilter": false,
		"bSort": false,
		"sAjaxSource": "/mcode/system/appraisal/queryPassingRate",
		"fnServerParams" : function(aDataSet) {
        	   aDataSet.push(
        			   {"name" : "years","value":$("#passId").val()});},
           "fbServerData":function(sSource,aDataSet){$.ajax({"dataType":"json","type":"GET","url":sSource,"data":aDataSet});},
           "aoColumns":[
                        {"mDataProp":"organName"},
                        {"mDataProp":"subjectName"},
                        {"mDataProp":"joinNum"},
                        {"mDataProp":"passNum"},
                        {"mDataProp":"passingRate"}],	
           "aoColumnDefs": [{"aTargets":[4],"mRender":function(data,type,full){return data+"%"}}],      				       
           "oLanguage":{"sUrl": "../resources/assets/plugins/dataTables/txt/page.txt"}
     });
	   	
	  
	   	passingGraph();
		$("#passId").on('change',function(){
			reloadrateDataTables();
			var passId = $("#passId").val();
			passingGraph(passId);
		});
});

//培训机构开班培训科目统计饼状图
function numGraph(years){
	var subjectlist=[];
	$.ajax({
		url:"/mcode/system/class/querySubject",
		dataType:"json",
		type:"POST", 
		success:function(data){
		for (var i= 0; i< data.length; i++) {
			var temp={value:data[i].dictKey,name:data[i].dictVal};
			subjectlist.push(temp);
		}
		 var Charts = echarts.init(document.getElementById('number'));
		 var json = [];
		 for(var i=0;i<subjectlist.length;i++){
			 $.ajax({
					url:"/mcode/system/class/querynum",
					dataType:"json",
					type:"POST",
					async:false,
					data: { 
						   "key":subjectlist[i].value,
						   "years":years
					},
					success:function(data){
						subjectlist[i]["value"]=data;
						json.push(subjectlist[i]);
	      var option = {  
		               title : {  
		                   text: '培训机构开班培训科目',  
		                   subtext: '数量统计',  
		                   x:'center'  
		               },  
		               tooltip : {  
		                   trigger: 'item',  
		                   formatter: "{a} <br/>{b} : {c} 个班"  
		               },  
		               toolbox: {  
		                   show : true,  
		                   feature : {  
		                       mark : {show: true},  
		                       dataView : {show: true, readOnly: false},  
		                       magicType : {  
		                           show: true,   
		                           type: ['pie', 'funnel'],  
		                           option: {  
		                               funnel: {  
		                                   x: '25%',  
		                                   width: '50%',  
		                                   funnelAlign: 'left',  
		                                   max: 1548  
		                               }  
		                           }  
		                       },  
		                       restore : {show: true},  
		                       saveAsImage : {show: true}  
		                   }  
		               },  
		               calculable : true,  
		               series : [  
		                   {  
		                       name:'培训科目',  
		                       type:'pie',  
		                       radius : '55%',//饼图的半径大小  
		                       center: ['50%', '60%'],//饼图的位置  
		                       data:json  
		                   }  
		               ]  
		         };   
		    Charts.setOption(option);
					}
			 });
		 }
		}
	});
}
//科目报名人数柱状图
function classGraph(years){
	var subjectNames=[],sums=[],datalist;
	$.ajax({
		type : "post", 
        url : '/mcode/system/class/querySumBySubjectId', 
        data:{"years":years},
        async : false, 
        success : function(data){
        	datalist=data;

	for(var i =0; i<datalist.length;i++){
		subjectNames.push(datalist[i].subjectName);
		sums.push(datalist[i].sum);
	}
	var classGraph = echarts.init(document.getElementById('site_statistics'));
	classGraph.setOption(
			option = {
				    tooltip : {
				        trigger: 'axis',
				        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
				            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
				        },
				    },
				    grid: {
				        left: '3%',
				        right: '4%',
				        bottom: '3%',
				        containLabel: true
				    },
				    xAxis: {
				        type : 'category',
				        splitLine: {show:false},
				        data : subjectNames
				    },
				    yAxis: {
				        type : 'value'
				    },
				    series: [
				      
				        {
				            name: '人数',
				            type: 'bar',
				            stack: '总量',
				            label: {
				                normal: {
				                    show: true,
				                    position: 'inside'
				                }
				            },
				            data:sums
				        }
				    ]
				}
			
			
			);
        }
	});
}
//机构通过率统计图
function passingGraph(passId){
 	var organNames=[],passingRates=[],datalist;
	$.ajax({
		type : "post", 
        url : '/mcode/system/appraisal/queryPassingRateChart', 
        data:{"years":passId},
        async : false, 
        success : function(data){
        	datalist=data;
        }
	});
	for(var i =0; i<datalist.length;i++){
		organNames.push(datalist[i].organName);
		passingRates.push(datalist[i].passingRate);
	}
	var passingGraph = echarts.init(document.getElementById('pass_statistics'));
	passingGraph.setOption(
			option = {
				    tooltip : {
				        trigger: 'axis',
				        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
				            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
				        },
				        formatter: function (params) {
				        	console.log(params[0]);
				            var tar = params[0];
				            return tar.name + '<br/>' + tar.seriesName + ' : ' + tar.value+'%';
				        }
				    },
				    grid: {
				        left: '3%',
				        right: '4%',
				        bottom: '3%',
				        containLabel: true
				    },
				    xAxis: {
				        type : 'category',
				        splitLine: {show:false},
				        data : organNames
				    },
				    yAxis: {
				        type : 'value',
				        axisLabel: {
			                  show: true,
			                  interval: 'auto',
			                  formatter: '{value} %'
			          		}
				    },
				    series: [
				      
				        {
				            name: '通过率',
				            type: 'bar',
				            stack: '总量',
				            label: {
				                normal: {
				                    show: true,
				                    position: 'inside'
				                }
				            },
				            data:passingRates
				        }
				    ]
				}
			);
}

function reloadrateDataTables(){
	rateDataTables.ajax.reload();
}

var allFn = function () {
	return {
		bottomFn_2 : function(flag) {
			var myCharts = echarts.init(document.getElementById(flag));
			var option = {
					 title : {
					        text: '培训学员年龄分布',
					       /* subtext: '纯属虚构',*/
					        x:'center',
					    },
					    tooltip : {
					        trigger: 'item',
					        formatter: "{a} <br/>{b} : {c} ({d}%)"
					    },
					    legend: {
					        orient: 'vertical',
					        left: 'left',
					        data: []
					    },
					    series : [
					        {
					            name: '各年龄段学员人数比例',
					            type: 'pie',
					            radius : '55%',
					            center: ['50%', '60%'],
					            data:[
					            ],
					            itemStyle: {
					                emphasis: {
					                    shadowBlur: 10,
					                    shadowOffsetX: 0,
					                    shadowColor: 'rgba(0, 0, 0, 0.5)'
					                }
					            }
					        }
					    ]
			};
			myCharts.setOption(option);
			// 各年龄段学员人数
			myCharts.showLoading();
			 $.ajax({
	                type : "post",
	                async : true,            //异步请求
	                url : "/mcode/system/student/queryStudentCountByAge",
	                data : {},
	                dataType : "json",
	                success : function(result) {
	                    fn_count++;
	                    if (result) {
	                        myCharts.hideLoading();    //隐藏加载动画
	                         myCharts.setOption({        //加载数据图表
	                            
	                             legend:{
	                                 data:['18岁以下','18岁-29岁','30岁-39岁','40岁-49岁','50岁以上'],
	                                 icon: 'circle',
	                                 orient: 'vertical',
	                             },
	                             series:[{data:result}]
	                         });
	                        window.addEventListener("resize",function(){
	                            myCharts.resize();
	                        });

	                    }

	                },
	                error : function(errorMsg) {
	                    //请求失败时执行该函数
	                    alert("年龄分布图表请求数据失败!");
	                    myCharts.hideLoading();
	                }
	            })
		}
}
}();